//
// Created by LiuYou on 2021/5/7.
//

#include <fcntl.h>
#include <arpa/inet.h>
#include "ClientData.hpp"

int Server::setNonBlocking(int fd) {
    // 获取文件状态标志。
    int oldOption = fcntl(fd, F_GETFL);
    // 设计新的文件状态是非阻塞的。
    int newOption = oldOption | O_NONBLOCK;
    fcntl(fd, F_SETFL, newOption);
    return oldOption;
}

const sockaddr_in& Server::ClientData::getSockAddrIn() const {
    return sockAddrIn;
}

void Server::ClientData::setSockAddrIn(const sockaddr_in& sockAddrIn) {
    ClientData::sockAddrIn = sockAddrIn;
}

//char* Server::ClientData::getWriteBuffer() const {
//    return writeBuffer;
//}
//
//void Server::ClientData::setWriteBuffer(char* writeBuffer) {
//    ClientData::writeBuffer = writeBuffer;
//}

const char* Server::ClientData::getBuffer() const {
    return buffer;
}

char* Server::ClientData::getBuffer() {
    return buffer;
}

const std::string& Server::ClientData::getUserName() const {
    return userName;
}

void Server::ClientData::setUserName(const std::string& userName) {
    ClientData::userName = userName;
}

const char* Server::ClientData::getIp() const {
    // TODO 注意: 不知道为什么这里不能转换。
//    inet_ntop(AF_INET, &sockAddrIn.sin_addr, ip, INET_ADDRSTRLEN);
    return ip;
}

Server::ClientData::ClientData() = default;
